package client;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import java.util.ArrayList;

import client.activity.PhotoPickerPActivity;

/**
 * 项目名称：client
 * 创建人:LuoFG
 * 创建时间:2021/9/15  14:24
 * 描述:
 * 作用:
 */
public class PhotoPreviewClient {
    private static class InstanceHolder {
        private static PhotoPreviewClient instance = new PhotoPreviewClient();
    }

    private static PhotoPreviewClient getInstance() {
        return PhotoPreviewClient.InstanceHolder.instance;
    }

    public static PhotoPreviewClient getClient() {
        return getInstance();
    }

    /***配置资源-Start***/
    //图片的数组
    private ArrayList<String> selectPhotoList;
    //当前选择的图片索引
    private int mCurrentPosition = 0;
    /***配置资源-End***/
    //传入具体包名类  作为区分
    private String PhotoResultTag = "";

    /***标题栏配置资源-Start***/
    //默认Bar文字白色
    private int barTextColor = R.color.White;
    //默认Bar背景白色
    private int barBgColor = R.color.Blue;
    //是否显示更多
    private boolean isShowMore = false;
    //是否显示标题栏
    private boolean isShowTitle = false;
    //是否一直显示标题栏
    private boolean isAlwaysShowTitle = false;
    //标题栏 标题
    private String strTitle = "";
    /***标题栏配置资源-End***/

    //设置成为默认属性
    public PhotoPreviewClient setDefault() {
        //默认Bar文字白色
        barTextColor = R.color.White;
        //默认Bar背景白色
        barBgColor = R.color.Blue;
        //已选图片的数组（根据需求，一般为空）
        selectPhotoList = null;
        //当前选择的图片索引
        mCurrentPosition = 0;
        //传入具体包名类  作为区分
        PhotoResultTag = "";
        //是否显示更多 仅图片预览使用
        isShowMore = false;
        //是否显示标题栏
        isShowTitle = false;
        //标题栏 标题
        strTitle = "图片预览";
        //是否一直显示标题栏
        isAlwaysShowTitle = false;
        return this;
    }

    /***异步保存图片-Start***/
    public interface SaveShowImageListener {
        void save();
    }
    private SaveShowImageListener saveShowImageListener;

    //运行Fragment的保存图片方法
    public void runSaveShowImage() {
        if (saveShowImageListener != null) {
            saveShowImageListener.save();
        }
    }
    //Fragment注册监听 才能执行保存图片的方法
    public void addSaveShowImageListener(SaveShowImageListener saveShowImageListener) {
        this.saveShowImageListener = saveShowImageListener;
    }

    /***异步保存图片-end***/


    /***保存图片  本地需要注册的监听-Start***/
    public interface GetSaveShowImageListener {
        void save(String path);
    }
    private GetSaveShowImageListener getSaveShowImageListener;

    //运行其他注册保存图片的方法地方
    public void runGetSaveShowImage(String path) {
        if (getSaveShowImageListener != null) {
            getSaveShowImageListener.save(path);
        }
    }
    //拿到图片后传递给注册保存图片方法的地方
    public PhotoPreviewClient addGetSaveShowImageListener(GetSaveShowImageListener getSaveShowImageListener) {
        this.getSaveShowImageListener = getSaveShowImageListener;
        return this;
    }

    public GetSaveShowImageListener getGetSaveShowImageListener() {
        return getSaveShowImageListener;
    }

    /***保存图片  本地需要注册的监听-end***/


    public interface MoreClickListener {
        void click(PhotoMoreResultBean photoMoreResultBean);
    }

    private MoreClickListener moreClickListener;

    //点击更多后  返回点击监听
    public void runMoreClickLister(PhotoMoreResultBean photoMoreResultBean) {
        if (moreClickListener != null) {
            moreClickListener.click(photoMoreResultBean);
        }
    }
    //添加更多的点击监听
    public PhotoPreviewClient addMoreClickListener(MoreClickListener moreClickListener) {
        this.moreClickListener = moreClickListener;
        return this;
    }

    public String getPhotoResultTag() {
        return PhotoResultTag;
    }

    public PhotoPreviewClient setPhotoResultTag(String photoResultTag) {
        PhotoResultTag = photoResultTag;
        return this;
    }

    public int getBarTextColor() {
        return barTextColor;
    }

    public PhotoPreviewClient setBarTextColor(int barTextColor) {
        this.barTextColor = barTextColor;
        return this;
    }

    public int getBarBgColor() {
        return barBgColor;
    }

    public PhotoPreviewClient setBarBgColor(int barBgColor) {
        this.barBgColor = barBgColor;
        return this;
    }

    public PhotoPreviewClient setShowMore(boolean showMore) {
        isShowMore = showMore;
        return this;
    }
    public boolean isShowMore() {
        return isShowMore;
    }

    public boolean isAlwaysShowTitle() {
        return isAlwaysShowTitle;
    }

    public PhotoPreviewClient setAlwaysShowTitle(boolean alwaysShowTitle) {
        isAlwaysShowTitle = alwaysShowTitle;
        return this;
    }

    public PhotoPreviewClient setSelectPhotoList(ArrayList<String> selectPhotoList) {
        this.selectPhotoList = selectPhotoList;
        return this;
    }

    public PhotoPreviewClient setSelectPhotoList(String url) {
        selectPhotoList=new ArrayList<>();
        if(!TextUtils.isEmpty(url)) {
            selectPhotoList.add(url);
        }
        return this;
    }

    public PhotoPreviewClient setmCurrentPosition(int mCurrentPosition) {
        this.mCurrentPosition = mCurrentPosition;
        return this;
    }

    public ArrayList<String> getSelectPhotoList() {
        return selectPhotoList;
    }

    public int getmCurrentPosition() {
        return mCurrentPosition;
    }

    public boolean isShowTitle() {
        return isShowTitle;
    }

    public PhotoPreviewClient setShowTitle(boolean showTitle) {
        isShowTitle = showTitle;
        return this;
    }

    public String getStrTitle() {
        return strTitle;
    }

    public PhotoPreviewClient setStrTitle(String strTitle) {
        this.strTitle = strTitle;
        return this;
    }

    public void start(Activity activity) {
        try {
            activity.startActivity(new Intent(activity, PhotoPickerPActivity.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}